<?
    function save_access_token($user_id, $access_token)
    {
        $db = new DB_GEAR();
        
        //set query
        $aQuery['Type']   = 'select';
        $aQuery['Column'] = array('*');
        $aQuery['Table']  = 'garden_auth_tbl';
        $aQuery['Where']  = 'user_id = "'.$user_id.'" and type = "mixi"';

        //set query
        $db->set_query($aQuery);

        //send query
        $result = $db->send_query(1);

        $db->clear_query();

        if($result['cnt'] == 0)
        {
            //set query
            $aQuery['Type'] = 'insert';
            $aQuery['Column'] = array(
                                      'user_id'      => $user_id, 
                                      'access_token' => $access_token, 
                                      'type'         => 'mixi',
            );
        }
        else
        {
            //set query
            $aQuery['Type'] = 'update';
            $aQuery['Column'] = array(
                                      'user_id'      => $user_id, 
                                      'access_token' => $access_token, 
                                      'type'         => 'mixi',
            );
            $aQuery['Where']  = 'user_id = "'.$user_id.'" and type = "mixi"';
        }

        $aQuery['Table'] = 'garden_auth_tbl';

        //set query
        $db->set_query($aQuery);
        
        //echo $db->get_query();

        //send query
        //echo $db->get_query();
        $result = $db->send_query(1);

        //close DB
        $db->close_DB();

        if($result['cnt'] == 0)
        {
            //echo 'NG';
            //return false;
        }
        else
        {
            //echo 'OK';
            //return true;
        }
    }

    function save_refresh_token($user_id, $refresh_token)
    {
        $db = new DB_GEAR();
        
        //set query
        $aQuery['Type']   = 'select';
        $aQuery['Column'] = array('*');
        $aQuery['Table']  = 'garden_auth_tbl';
        $aQuery['Where']  = 'user_id = "'.$user_id.'" and type = "mixi"';

        //set query
        $db->set_query($aQuery);

        //send query
        $result = $db->send_query(1);

        $db->clear_query();

        if($result['cnt'] == 0)
        {
            //set query
            $aQuery['Type']   = 'insert';
            $aQuery['Column'] = array(
                                      'user_id'      => $user_id, 
                                      'refresh_token' => $refresh_token, 
                                      'type'         => 'mixi',
            );
        }
        else
        {
            //set query
            $aQuery['Type']   = 'update';
            $aQuery['Column'] = array(
                                      'user_id'      => $user_id, 
                                      'refresh_token' => $refresh_token, 
                                      'type'         => 'mixi',
            );
            $aQuery['Where']  = 'user_id = "'.$user_id.'" and type = "mixi"';
        }
        $aQuery['Table'] = 'garden_auth_tbl';

        //set query
        $db->set_query($aQuery);
        //echo $db->get_query();

        //send query
        //echo $db->get_query();
        $result = $db->send_query(1);

        //close DB
        $db->close_DB();

        if($result['cnt'] == 0)
        {
            //echo 'NG';
            //return false;
        }
        else
        {
            //echo 'OK';
            //return true;
        }
    }

    function save_c2dm_registation_id($user_id, $registation_id){
        $db = new DB_GEAR();
        
        //set query
        $aQuery['Type']   = 'update';
        $aQuery['Column'] = array(
                                  'registation_id' => $registation_id
        );
        $aQuery['Where']  = 'user_id = "'.$user_id;
        $aQuery['Table'] = 'garden_c2dm_tbl';

        //set query
        $db->set_query($aQuery);
        //echo $db->get_query();

        //send query
        //echo $db->get_query();
        $result = $db->send_query(1);

        //close DB
        $db->close_DB();

        if($result['cnt'] == 0)
        {
            //echo 'NG';
            //return false;
        }
        else
        {
            //echo 'OK';
            //return true;
        }
    }

function show_date($date)
{
	if(strtotime(substr($date, 0, 10)) == strtotime(date("Y/m/d")))
	{
		echo substr($date, 11, 5);
	}
	else
	{
		echo str_replace("-", "/",substr($date, 6, 4));
	}

	//if(strtotime(substr($date, 0, 10)) == strtotime(date("Y/m/d",strtotime("-1 day"))))
}

function old_invite($user_id, $pod_id, $invitee_id, $type = 'mixi')
{
    $db = new DB_GEAR();

	//set query
	$aQuery['Type']   = 'replace into';
	$aQuery['Column'] = array(
	                      'user_id' => $user_id,
	                      'pod_id' => $pod_id,
	                      'invitee_id' => $invitee_id,
	                      'type' => $type,
	                      'time_stamp' => date("Y-m-d h:m:s"),
	);
	$aQuery['Table']  = 'garden_invite_tbl';

    //set query
    $db->set_query($aQuery);

//echo $db->get_query();

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
    	//echo 'OK';
    }
    else
    {
    	//echo 'NG';
    }
}

function invite($user_id, $pod_id, $invitee_id, $type = 'mixi')
{
    $db = new DB_GEAR();

    //set query
	$aQuery['Type']   = 'select';
	$aQuery['Column'] = array(
	                          '*',
							 );
	$aQuery['Table']  = 'garden_invite_tbl';
	$aQuery['Where']  = 'invitee_id = "'.$invitee_id.'" and pod_id = "'.$pod_id.'"';
    
    //set query
    $db->set_query($aQuery);
    //echo 'xxxxxxx'.$db->get_query();

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
        	//echo 'NG';
    }
    else
    {
    	//echo 'OK';
	    $db->clear_query();
	    
		//set query
		$aQuery['Type']   = 'insert';
		$aQuery['Column'] = array(
		                      'user_id'    => $user_id,
		                      'pod_id'     => $pod_id,
		                      'invitee_id' => $invitee_id,
		                      'type'       => $type,
		                      //'time_stamp' => date("Y-m-d h:m:s"),
		);
		$aQuery['Table']  = 'garden_invite_tbl';


	    //set query
	    $db->set_query($aQuery);
	    //echo 'xxxxxxx'.$db->get_query();

	    //send query
	    $result = $db->send_query(1);
    	//return $result[1]['last_insert_id()'];
    }
}

function is_login()
{
    if($_SESSION['status'] == 'login')
    {
        return true;
    }
    else
    {
        return false;
    }
}

function login($user_id)
{
    //pend
}

function make_pod($name)
{
    $db = new DB_GEAR();

	//set query
	$aQuery['Type']   = 'insert';
	$aQuery['Column'] = array(
	                      'name' => $name,
	);
	$aQuery['Table']  = 'garden_pod_tbl';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);
    
    $db->clear_query();
    
    //set query
	$aQuery['Type']   = 'select';
	$aQuery['Column'] = array(
	                      'distinct last_insert_id()',
	);
	$aQuery['Table']  = 'garden_pod_tbl';

    //set query
    $db->set_query($aQuery);
    //echo 'xxxxxxx'.$db->get_query();

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
    	//echo 'OK';
    	return $result[1]['last_insert_id()'];
    }
    else
    {
    	//echo 'NG';
    }
}

function get_using_user($user_id, $type = 'mixi')
{
	$url = 'http://reafo.net/garden/using_list.php';
	$data = array(
	    'user_id'              => $user_id,
	    'g_key_din39s03j23ogn' => G_KEY,
	);

	$headers = array(
	    'Authorization: OAuth '.'todoke', 
	);

	$options = array('http' => array(
	    'method' => 'POST',
	    'content' => http_build_query($data),
	    'header' => implode("\r\n", $headers),
	));

	$contents = file_get_contents($url, false, stream_context_create($options));

    return $contents;
}

function is_pod_member($user_id, $pod_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_pod_members_tbl';
    $aQuery['Where']  = 'user_id = "'.$user_id.'" and pod_id = "'.$pod_id.'"';

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query(1);
    
    if($result['cnt'] == 1)
    {
    	//echo 'OK';
    	return true;
    }
    else
    {
    	return false;
    }
}

function get_mixi_user($user_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_mixi_tbl';
    $aQuery['Where']  = 'user_id = "'.$user_id.'" and using_flag = 0';

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query(1, 1, 1);
    
    return $result;
}

function get_pod_list($user_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'select pod.* from garden_pod_members_tbl as member inner join garden_pod_tbl as pod on member.pod_id = pod.pod_id where member.user_id = '.$user_id.' ORDER BY pod.pod_id DESC';

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query(1);

	if(is_null($result) == false){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$aList[] = $rec;
		}
		
		return $aList;
	}else{
		return false;
	}

    return $aList;

}

function add_pod_members($user_id, $pod_id)
{
	$url = 'http://reafo.net/garden/pod.php';
	$data = array(
	    'user_id'               => $user_id,
	    'pod_id'               => $pod_id,
	    'type'                 => 'add_pod_member',
	    'g_key_din39s03j23ogn' => G_KEY,
	);

	$headers = array(
	    'Authorization: OAuth '.'todoke', 
	);

	$options = array('http' => array(
	    'method' => 'POST',
	    'content' => http_build_query($data),
	    'header' => implode("\r\n", $headers),
	));

	$contents = file_get_contents($url, false, stream_context_create($options));

    return $contents;
}

function old_add_pod_members($user_id, $pod_id)
{
    $db = new DB_GEAR();

	//set query
	$aQuery['Type']   = 'replace into';
	$aQuery['Column'] = array(
	                      'pod_id' => $pod_id,
	                      'user_id' => $user_id,
	);
	$aQuery['Table']  = 'garden_pod_members_tbl';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
    	echo 'OK';
    }
    else
    {
    	echo 'NG';
    }
}


function post($user_id, $text, $pod_id, $via, $type =0)
{
    $db = new DB_GEAR();

	//set query
	$aQuery['Type']   = 'insert';
	$aQuery['Column'] = array(
	                      'user_id'  => $user_id,
	                      'text'     => $text,//mb_convert_encoding($text,"UTF-8"),
	                      'pod_id'   => $pod_id,
	                      'type'     => $type,
	                      'via'      => $via,
	);
	$aQuery['Table']  = 'garden_contents_tbl';

    //set query
    $db->set_query($aQuery);

    //send query
    $db->send_query(1);

    $db->clear_query();
    
    //set query
	$aQuery['Type']   = 'select';
	$aQuery['Column'] = array(
	                      'distinct last_insert_id()',
	);
	$aQuery['Table']  = 'garden_contents_tbl';

    //set query
    $db->set_query($aQuery);
    //echo 'xxxxxxx'.$db->get_query();

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
    	//echo 'OK';
	    $db->clear_query();
	    
	    //set query
		$aQuery['Type']   = 'insert';
		$aQuery['Column'] = array(
		                      'user_id'    => $user_id,
		                      'content_id' => $result[1]['last_insert_id()'],
		                      'pod_id'     => $pod_id,
		);
		$aQuery['Table']  = 'garden_feed_tbl';

	    //set query
	    $db->set_query($aQuery);
	    //echo 'xxxxxxx'.$db->get_query();

	    //send query
	    $result = $db->send_query(1);
    	//return $result[1]['last_insert_id()'];
    }
    else
    {
    	//echo 'NG';
    }
}


function get_feed($user_ids, $pod_ids)
{
    $db = new DB_GEAR();

	//set query
	/*
	$aQuery['Type']   = 'select';
	$aQuery['Column'] = array('*');
	$aQuery['Table']  = 'garden_contents_tbl';
	$aQuery['Where']  = 'pod_id IN ('.$pod_ids.') or (user_id IN ('.$user_ids.') and pod_id = 0)';
	$aQuery['OrderBy']  = 'time_stamp DESC';
	*/

    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'SELECT * FROM garden_contents_tbl as contents left join garden_user_tbl as user on contents.user_id = user.user_id WHERE pod_id IN ('.$pod_ids.') or ( contents.user_id IN ('.$user_ids.') and contents.pod_id = 0) ORDER BY time_stamp DESC LIMIT 0, 10';


    //set query
    $db->set_query($aQuery);
    
    //echo $db->get_query();

    //send query
    $result = $db->send_query(1, 1, 1);

	if($result != false){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$data[] = $rec;
		}
	}
	else
	{
		$data = false;
	}

	return $data;

    //$db->clear_query();

	//SELECT * FROM `garden_contents_tbl` WHERE content_id IN (1, 4, 6) or (user_id IN (2, 4, 5) and pod_id = 0)
}

function get_open_stream($user_ids)
{
    $db = new DB_GEAR();

	//set query
	/*
	$aQuery['Type']   = 'select';
	$aQuery['Column'] = array('*');
	$aQuery['Table']  = 'garden_contents_tbl';
	$aQuery['Where']  = 'user_id IN ('.$user_ids.') and pod_id = 0';
	$aQuery['OrderBy']  = 'time_stamp DESC';
	*/

    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'SELECT * FROM garden_contents_tbl as contents left join garden_user_tbl as user on contents.user_id = user.user_id WHERE contents.user_id IN ('.$user_ids.') and contents.pod_id = 0 ORDER BY time_stamp DESC';

    //set query
    $db->set_query($aQuery);
    
    //echo $db->get_query();

    //send query
    $result = $db->send_query(1, 1, 1);

	if($result){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$data[] = $rec;
		}
	}
	else
	{
		$data = false;
	}

	return $data;

    //$db->clear_query();

	//SELECT * FROM `garden_contents_tbl` WHERE content_id IN (1, 4, 6) or (user_id IN (2, 4, 5) and pod_id = 0)
	//SELECT * FROM garden_contents_tbl as contents left join garden_user_tbl as user on contents.user_id = user.user_id WHERE pod_id IN (3, 2, 1) or (contents.user_id IN (4, 2, 1) and contents.pod_id = 0) ORDER BY time_stamp DESC
}


function get_contents($pod_id)
{
	$url = 'http://reafo.net/garden/pod.php';
	$data = array(
	    'pod_id'               => $pod_id,
	    'type'                 => 'pod_contents',
	    'g_key_din39s03j23ogn' => G_KEY,
	);

	$headers = array(
	    'Authorization: OAuth '.'todoke', 
	);

	$options = array('http' => array(
	    'method' => 'POST',
	    'content' => http_build_query($data),
	    'header' => implode("\r\n", $headers),
	));

	$contents = file_get_contents($url, false, stream_context_create($options));

    return $contents;
}

function pod_member_list($pod_id)
{
	$url = 'http://reafo.net/garden/pod.php';
	$data = array(
	    'pod_id'               => $pod_id,
	    'type'                 => 'pod_member',
	    'g_key_din39s03j23ogn' => G_KEY,
	);

	$headers = array(
	    'Authorization: OAuth '.'todoke', 
	);

	$options = array('http' => array(
	    'method' => 'POST',
	    'content' => http_build_query($data),
	    'header' => implode("\r\n", $headers),
	));

	$contents = file_get_contents($url, false, stream_context_create($options));

    return $contents;
}

function old_get_contents($pod_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_contents_tbl';
    $aQuery['Where']  = 'pod_id = "'.$pod_id.'"';

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query(1, 1, 1);
    
    return $result;
}

function load_user($user_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_user_tbl';
    $aQuery['Where']  = 'user_id = "'.$user_id.'"';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1, 1, 1);
    
    return $result;
}

function old_add_contact_list($user_id, $mixi_id, $name, $profile_image = null, $mail = null, $phone_number = null)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_user_tbl';
    $aQuery['Where']  = 'mixi_id = "'.$mixi_id.'"';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);

    if($result['cnt'] == 1)
    {
        $use_flag = 1;
    }
    else
    {
        $use_flag = 0;
    }

    $db->clear_query();

    //set query
    $aQuery['Type']   = 'insert';
    $aQuery['Column'] = array(
                              'user_id'       => $user_id, 
                              'mixi_id'       => $mixi_id, 

                              'name'          => $name,
                              'profile_image' => profile_image,

                              'mail_address'  => mail_address,

                              'phone_number'  => $phone_number,

                              'use_flag'      => $use_flag,

    );
    $aQuery['Table'] = 'garden_contact_list';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);
}

function update_using($user_id, $mixi_id, $type = 'mixi')
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'update';
    $aQuery['Column'] = array(
    						  'using_flag' => $user_id,
    						  );
    $aQuery['Table']  = 'garden_mixi_tbl';
    $aQuery['Where']  = 'mixi_id = "'.$mixi_id.'"';

    //set query
    $db->set_query($aQuery);

	//echo $db->get_query();

    //send query
    $result = $db->send_query(1);
}

function add_mixi_list($user_id, $mixi_id, $name, $profile_image = null)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('*');
    $aQuery['Table']  = 'garden_user_tbl';
    $aQuery['Where']  = 'mixi_id = "'.$mixi_id.'"';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);
    
    $db->clear_query();

    if($result['cnt'] == 1)
    {
        $using_flag = $result[1]['user_id'];
    }
    else
    {
        $using_flag = 0;
    }

    $db->clear_query();

    //set query
    $aQuery['Type']   = 'insert';
    $aQuery['Column'] = array(
                              'user_id'       => $user_id, 
                              'mixi_id'       => $mixi_id, 
                              'name'          => $name,
                              'profile_image' => $profile_image,
                              'using_flag'    => $using_flag,

    );
    $aQuery['Table'] = 'garden_mixi_tbl';

    //set query
    $db->set_query($aQuery);

    //send query
    $result = $db->send_query(1);
}

function get_invite_member($pod_id, $type = 'all')
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'direct';
    $aQuery['Query']  = 'SELECT DISTINCT mixi.name, mixi.profile_image FROM garden_invite_tbl as invite inner join garden_mixi_tbl as mixi on invite.invitee_id = mixi.mixi_id WHERE invite.pod_id = '.$pod_id;

    //set query
    $db->set_query($aQuery);
    //echo $db->get_query();
    //send query
    $result = $db->send_query(1);

	if(is_null($result) == false){
		while($rec = mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$aList[] = $rec;
		}
		
		return $aList;
	}else{
		return false;
	}

}

function handling_invite($user_id, $invitee_id)
{
    $db = new DB_GEAR();

    //set query
    $aQuery['Type']   = 'select';
    $aQuery['Column'] = array('pod_id');
    $aQuery['Table']  = 'garden_invite_tbl';
    $aQuery['Where']  = 'invitee_id = "'.$invitee_id.'" and type = "mixi"';

    //set query
    $db->set_query($aQuery);
	//echo $db->get_query();

    //send query
    $result = $db->send_query(1);
    
    $db->clear_query();
	
    if($result['cnt'] > 0)
    {
        foreach($result[1] as $invite_pod)
        {
        	add_pod_members($user_id, $invite_pod['pod_id']);
        	//echo $user_id.':'.$invite_pod['pod_id'];
        }

	    //set query
	    $aQuery['Type']   = 'delete';
	    $aQuery['Column'] = array('pod_id');
	    $aQuery['Table']  = array('garden_invite_tbl');
	    $aQuery['Where']  = 'invitee_id = "'.$invitee_id.'" and type = "mixi"';

	    //set query
	    $db->set_query($aQuery);
		echo $db->get_query();

	    //send query
	    $result = $db->send_query(1);

    }
    else
    {

    }
}


function set_session($data, $access_token = null, $refresh_token = null)
{
  $_SESSION['user_id']            = $data['user_id'];
  $_SESSION['mixi_id']            = $data['mixi_id'];
  $_SESSION['facebook_id']        = $data['facebook_id'];
  $_SESSION['twitter_id']         = $data['twitter_id'];
  $_SESSION['c2dm_id']            = $data['c2dm_id'];
  $_SESSION['user_name']          = $data['user_name'];
  $_SESSION['profile_image']      = $data['profile_image'];
  $_SESSION['g_session']          = $data['g_session'];
  $_SESSION['expire']             = $data['expire'];
  $_SESSION['status']             = $data['status'];
  $_SESSION['mixi_access_token']  = $access_token;
  $_SESSION['mixi_refresh_token'] = $refresh_token;
}

function load_session()
{
  $data['user_id']            = $_SESSION['user_id'];
  $data['mixi_id']            = $_SESSION['mixi_id'];
  $data['facebook_id']        = $_SESSION['facebook_id'];
  $data['twitter_id']         = $_SESSION['twitter_id'];
  $data['c2dm_id']            = $_SESSION['c2dm_id'];
  $data['user_name']          = $_SESSION['user_name'];
  $data['profile_image']      = $_SESSION['profile_image'];
  $data['g_session']          = $_SESSION['g_session'];
  $data['expire']             = $_SESSION['expire'];
  $data['status']             = $_SESSION['status'];
  $data['mixi_access_token']  = $_SESSION['mixi_access_token'];
  $data['mixi_refresh_token'] = $_SESSION['mixi_refresh_token'];
  
  return $data;
}

?>
